<template>
  <div class="login">
    <div class="vue-form">
      <h3>{{ title }}</h3>
      <el-form :model="formData" ref="loginForm" :rules="rules">
        <el-form-item prop="userName">
          <el-input
            v-model="formData.userName"
            placeholder="请输入用户名"
            prefix-icon="User"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formData.password"
            showPassword
            prefix-icon="Lock"
          />
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="formData.rememberPwd" label="记住密码" />
        </el-form-item>
        <el-form-item>
          <el-button
            v-blur
            style="width: 100%"
            :loading="loginLoading"
            type="primary"
            @click="loginSystem"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { encrypt, decrypt } from "@/utils/jsencrypt.js";
import { useUserStore } from "@/stores/modules/user.js";

const router = useRouter();
const userStore = useUserStore();

const loginForm = ref();
const loginLoading = ref(false);
const title = ref(import.meta.env.VITE_SYSTEM_TITLE);
const formData = ref({
  ...userStore.userInfo,
  password: decrypt(userStore.userInfo.password),
});

const rules = ref({
  userName: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
});
const loginSystem = () => {
  if (!loginForm.value) return;
  loginForm.value.validate((valid, fields) => {
    if (valid) {
      loginLoading.value = true;
      ElMessage.success("登录成功！");
      if (formData.value.rememberPwd) {
        userStore.userInfo = {
          ...formData.value,
          password: encrypt(formData.value.password),
        };
      } else {
        userStore.userInfo = {
          userName: "",
          password: "",
          rememberPwd: formData.value.rememberPwd,
        };
      }

      userStore
        .getInfo()
        .then(() => {
          setTimeout(() => {
            loginLoading.value = false;
            router.push("/");
          }, 1000);
        })
        .catch(() => {
          loginLoading.value = false;
        });
    } else {
      console.log("error submit!", fields);
    }
  });
};
</script>
<style lang="scss" scoped>
.login {
  height: 100%;
  background-image: linear-gradient(
    to right,
    #0a3e8d,
    #003283,
    #051ca3,
    #0a3e8d
  );
  background-color: #0d47a1;
  display: flex;
  justify-content: center;
  align-items: center;
  #tsparticles {
    height: 100%;
    width: 100%;
  }
}
.vue-form {
  width: 350px;
  padding: 40px;
  background-color: rgba(#ffffff, 1);
  z-index: 1;
  border-radius: 10px;
  box-shadow: 0 0 6px 0 rgba(#6578e6, 0.5);
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;

  h3 {
    text-align: center;
    margin-bottom: 20px;
    // color: #fff;
  }
}
</style>
